<?php

/**
 * Description of Products
 *
 * @author Kopi
 */
class Products extends ModelAbstract{

    
    protected $fields = array(
        'id_products'	=> 'id',
        'name'		=> 'name',
        'description'	=> 'description',
        'price'		=> 'price',
        'date_create'	=> 'dateCreate',
        'date_update'	=> 'dateUpdate',
        
    );
    
    private $id;
    private $name;
    private $description;
    private $price;
    private $dateCreate;
    private $dateUpdate;
    
    private $testArray = array('fdsfsd',4,5);
    
    private $Producers; //producers obj
    //private $types;  //types collection
    
    public function __construct() {
	$this->Producers = new Producers();
    }


    public function getId() {
	return $this->id;
    }

    public function setId($id) {
	$this->id = $id;
    }

    public function getName() {
	return $this->name;
    }

    public function setName($name) {
	$this->name = $name;
    }

    public function getDescription() {
	return $this->description;
    }

    public function setDescription($description) {
	$this->description = $description;
    }

    public function getPrice() {
	return $this->price;
    }

    public function setPrice($price) {
	$this->price = $price;
    }

    public function getDateCreate() {
	return $this->dateCreate;
    }

    public function setDateCreate($dateCreate) {
	$this->dateCreate = $dateCreate;
    }

    public function getDateUpdate() {
	return $this->dateUpdate;
    }

    public function setDateUpdate($dateUpdate) {
	$this->dateUpdate = $dateUpdate;
    }

    public function getFields() {
	return $this->fields;
    }
    
    public function iteratechildren($object,$xml){
	// $xml = $object SimpleXMLElement("<$className></$className>"); 
	foreach ($object as $name => $value) {
	     if($name!='xml' && $name!='fields' && $value)
	     {
		 if (is_string($value) || is_numeric($value)) {
		     $xml->$name=$value;
		 } 
		 else if (is_array($value))
		 {
		     $xml->$name = new SimpleXMLElement("<$name></$name>");
		     foreach ($value as $arrayKey => $arrayValue) 
		     {
			 $item = $xml->$name->addChild('item',$arrayValue);
			 $item->addAttribute('key',$arrayKey);
				 
		     }
		 }
		 else {
		     $xml->$name = null;
		     $value->iteratechildren($value,$xml->$name);
		 }
	     }
         }
	 return $xml;
    }   
    
    public function iteratechildrenPath($object,$pathArray = array()){
	// $xml = $object SimpleXMLElement("<$className></$className>"); 
	foreach ($object as $name => $value) {
	     if($name!='xml' && $name!='fields' && $value)
	     {
		 if (is_array($value))
		 {
		     foreach ($value as $arrayKey => $arrayValue) 
		     {
			 $pathArray[__CLASS__ .'_' . $name] = $name . '/item';
				 
		     }
		 }
		 else if (is_object($value)){
		     //$xml->$name = null;
		     $pathArray[$name] = $name;
		     $pathArray = $value->iteratechildrenPath($value,$pathArray);
		 }
	     }
         }
	 return $pathArray;
    }    
     
    public function getXmlFields($data = 'products')
    {
	$this->xml = new SimpleXMLElement("<$data id=\"" . $this->getId() . "\"></$data>");
	$this->xml = $this->iteratechildren($this,$this->xml);
	return str_replace('<?xml version="1.0"?>', '', $this->xml->asXML());
	
    }
    
    public function getPathArray($data = 'products')
    {
	
	$pathArray = $this->iteratechildrenPath($this);
	return $pathArray;
	
    }    
    
    public function getValue($field)
    {
	$name = 'get' . $field;
	return $this->$name();
    }
    
    public function getProducers() {
	return $this->Producers;
    }

    public function setProducers($producers) {
	$this->Producers = $producers;
    }

    public function getTypes() {
	return $this->types;
    }

    public function setTypes($types) {
	$this->types = $types;
    }

        
    public function getClassName()
    {
	return __CLASS__;
    }
    
    public function getLinkedClasses()
    {
	$objList = array();
	foreach ($this as $key => $value)
	{
	    if(is_object($value))
	    {
		$objList[$key] = $value;
	    }
	}
	
	return $objList;
    }
    
    public function getTestArray() {
	return $this->testArray;
    }

    public function setTestArray($testArray) {
	$this->testArray = $testArray;
    }

    
    
}

?>
